//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.qsadmin.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;

public class CmdConsole extends JPanel {
    private static final String NEW_LINE = "\r\n";
    private QSAdminMain qsadminMain;
    private JPanel centerPanel;
    private JPanel textPanel;
    private JPanel sendPanel;
    private JPanel buttonPanel;
    private JLabel convLabel = new JLabel("Conversation with host");
    private Border connectedBorder = BorderFactory.createTitledBorder(new EtchedBorder(), "Connected To < NONE >");
    private JTextArea messagesField = new JTextArea();
    private JLabel sendLabel = new JLabel("Message");
    private JTextField sendField = new JTextField();
    private JButton sendButton = new JButton("Send");
    private JButton saveButton = new JButton("Save");
    private JButton clearButton = new JButton("Clear");
    private GridBagConstraints gbc = new GridBagConstraints();

    public CmdConsole(QSAdminMain qsadminMain) {
        this.qsadminMain = qsadminMain;
        this.textPanel = new JPanel();
        this.textPanel.setLayout(new BorderLayout(0, 5));
        this.textPanel.add(this.convLabel, "North");
        this.messagesField.setEditable(false);
        this.messagesField.setBackground(Color.BLACK);
        this.messagesField.setForeground(Color.GREEN);
        JScrollPane jsp = new JScrollPane(this.messagesField);
        this.textPanel.add(jsp);
        this.textPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 3));
        this.sendPanel = new JPanel();
        this.sendPanel.setLayout(new GridBagLayout());
        this.gbc.weighty = 0.0D;
        this.gbc.weightx = 0.0D;
        this.gbc.gridx = 0;
        this.gbc.gridy = 0;
        this.gbc.gridheight = 1;
        this.gbc.gridwidth = 1;
        this.gbc.anchor = 13;
        this.gbc.fill = 0;
        this.sendPanel.add(this.sendLabel, this.gbc);
        this.gbc.gridx = 1;
        this.gbc.weightx = 1.0D;
        this.gbc.fill = 2;
        this.sendField.setEditable(false);
        this.sendPanel.add(this.sendField, this.gbc);
        this.gbc.gridx = 2;
        this.gbc.weightx = 0.0D;
        this.gbc.fill = 0;
        this.sendButton.setEnabled(false);
        this.sendButton.setToolTipText("Send text to host");
        ActionListener sendListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = CmdConsole.this.sendField.getText();
                if (!msg.equals("")) {
                    CmdConsole.this.sendMessage(msg);
                } else {
                    int value = JOptionPane.showConfirmDialog(CmdConsole.this, "Send Blank Line ?", "Send Data To Server", 0);
                    if (value == 0) {
                        CmdConsole.this.sendMessage(msg);
                    }
                }

            }
        };
        this.sendButton.addActionListener(sendListener);
        this.sendField.addActionListener(sendListener);
        this.sendPanel.add(this.sendButton, this.gbc);
        this.sendPanel.setBorder(new CompoundBorder(BorderFactory.createEmptyBorder(0, 0, 0, 3), BorderFactory.createTitledBorder("Send")));
        this.buttonPanel = new JPanel();
        this.buttonPanel.setLayout(new GridBagLayout());
        this.gbc.weighty = 0.0D;
        this.gbc.weightx = 1.0D;
        this.gbc.gridx = 0;
        this.gbc.gridy = 0;
        this.gbc.gridheight = 2;
        this.gbc.gridwidth = 1;
        this.gbc.anchor = 13;
        this.gbc.fill = 1;
        this.buttonPanel.add(this.sendPanel, this.gbc);
        this.gbc.weighty = 0.0D;
        this.gbc.weightx = 0.0D;
        this.gbc.gridx = 1;
        this.gbc.gridheight = 1;
        this.gbc.fill = 2;
        this.saveButton.setToolTipText("Save conversation with host to a file");
        this.saveButton.setMnemonic('S');
        ActionListener saveListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = CmdConsole.this.messagesField.getText();
                if (text.equals("")) {
                    CmdConsole.this.error("Nothing to save", "Save to file");
                } else {
                    String fileName = "";
                    JFileChooser chooser = new JFileChooser();
                    chooser.setCurrentDirectory(new File("."));
                    int returnVal = chooser.showSaveDialog(CmdConsole.this);
                    if (returnVal == 0) {
                        fileName = chooser.getSelectedFile().getAbsolutePath();

                        try {
                            CmdConsole.writeFile(fileName, text);
                        } catch (Exception var7) {
                            JOptionPane.showMessageDialog(CmdConsole.this, var7.getMessage(), "Error saving to file..", 0);
                        }
                    }

                }
            }
        };
        this.saveButton.addActionListener(saveListener);
        this.buttonPanel.add(this.saveButton, this.gbc);
        this.gbc.gridy = 1;
        this.clearButton.setToolTipText("Clear conversation with host");
        this.clearButton.setMnemonic('C');
        ActionListener clearListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CmdConsole.this.messagesField.setText("");
            }
        };
        this.clearButton.addActionListener(clearListener);
        this.buttonPanel.add(this.clearButton, this.gbc);
        this.buttonPanel.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 3));
        this.centerPanel = new JPanel();
        this.centerPanel.setLayout(new BorderLayout(0, 0));
        this.centerPanel.add(this.buttonPanel, "South");
        this.centerPanel.add(this.textPanel, "Center");
        CompoundBorder cb = new CompoundBorder(BorderFactory.createEmptyBorder(5, 10, 10, 10), this.connectedBorder);
        this.centerPanel.setBorder(cb);
        this.setLayout(new BorderLayout());
        this.add(this.centerPanel, "Center");
    }

    public void append(String msg) {
        this.setSendEdit(this.qsadminMain.isConnected());
        this.messagesField.append(msg + "\r\n");
        this.messagesField.setCaretPosition(this.messagesField.getText().length());
    }

    public void sendMessage(String s) {
        this.setCursor(Cursor.getPredefinedCursor(3));

        try {
            this.qsadminMain.sendCommand(s, true);
            this.sendField.setText("");
            this.setCursor(Cursor.getPredefinedCursor(0));
        } catch (Exception var3) {
            this.setCursor(Cursor.getPredefinedCursor(0));
            JOptionPane.showMessageDialog(this, var3.getMessage(), "Error Sending Message", 0);
        }

    }

    private void changeBorder(String ip) {
        if (ip != null && !ip.equals("")) {
            this.connectedBorder = BorderFactory.createTitledBorder(new EtchedBorder(), "Connected To < " + ip + " >");
        } else {
            this.connectedBorder = BorderFactory.createTitledBorder(new EtchedBorder(), "Connected To < NONE >");
        }

        CompoundBorder cb = new CompoundBorder(BorderFactory.createEmptyBorder(5, 10, 10, 10), this.connectedBorder);
        this.centerPanel.setBorder(cb);
        this.invalidate();
        this.repaint();
    }

    public void error(String error) {
        this.error(error, (String)null);
    }

    public void error(String error, String heading) {
        if (error != null && !error.equals("")) {
            if (heading == null) {
                heading = "Error";
            }

            JOptionPane.showMessageDialog(this, error, heading, 0);
        }
    }

    public static void writeFile(String fileName, String text) throws IOException {
        PrintWriter out = null;

        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
            out.print(text);
        } finally {
            if (out != null) {
                out.close();
            }

        }

    }

    public void setSendEdit(boolean flag) {
        this.sendButton.setEnabled(flag);
        this.sendField.setEditable(flag);
    }

    public void updateConnectionStatus(boolean connected) {
        if (!connected) {
            this.changeBorder((String)null);
        } else {
            this.changeBorder(this.qsadminMain.getIpAddress());
        }

    }
}
